package com.fenyin.samples.study.jdk.nio.buffer;  

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.junit.Test;
  
/**
 * ByteBuffer常用操作功能实例
 *@Author:fengzhichao  
 *@Since:2012-12-20  
 *@Version:1.1.0
 */
public class ByteBufferTest {
	
	private static final Log log = LogFactory.getLog(ByteBufferTest.class);
	
	private static void print(String key,ByteBuffer buffer){
		byte[] arrays = buffer.array();
		StringBuffer sb = new StringBuffer(key+":");
		for(byte b : arrays){
			sb.append(b).append("|");
		}
		sb.append(buffer.toString(  ));
		log.info(sb.toString());
	}
	
	private static void print(String key){
		log.info(key+":");
	}

	@Test
	public void resetTest(){
		ByteBuffer byteBuffer = ByteBuffer.allocate(20).order(ByteOrder.BIG_ENDIAN);
		print("resetTest-init-end",byteBuffer);
		byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'H');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'i');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("resetTest-put-7-end",byteBuffer);
        byteBuffer.limit(10);
        print("resetTest-limit-10-end",byteBuffer);
        byteBuffer.mark();
        print("resetTest-mark-end");
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("resetTest-put-2-end",byteBuffer);
        byteBuffer.reset();
        print("resetTest-reset-end",byteBuffer);
        
	}
	
	@Test
	public void clearTest(){
		ByteBuffer byteBuffer = ByteBuffer.allocate(20).order(ByteOrder.BIG_ENDIAN);
		print("clearTest-init-begin",byteBuffer);
//		  绝对
//		  byteBuffer.put (0, (byte)0);  
//        byteBuffer.put (1, (byte)'H');  
//        byteBuffer.put (2, (byte)0);  
//        byteBuffer.put (3, (byte)'i');  
//        byteBuffer.put (4, (byte)0);  
//        byteBuffer.put (5, (byte)'!');  
//        byteBuffer.put (6, (byte)0);  
		byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'H');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'i');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("clearTest-put-7-end",byteBuffer);
        byteBuffer.limit(10);
        print("clearTest-limit-10-end",byteBuffer);
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("clearTest-put-2-end",byteBuffer);
        byteBuffer.clear();
        print("clearTest-clear-end",byteBuffer);
	}
	
	@Test
	public void flipTest(){
		ByteBuffer byteBuffer = ByteBuffer.allocate(20).order(ByteOrder.BIG_ENDIAN);
		print("flipTest-init-begin",byteBuffer);
		byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'H');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'i');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("flipTest-put-7-end",byteBuffer);
        byteBuffer.limit(10);
        print("flipTest-limit-10-end",byteBuffer);
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("flipTest-put-2-end",byteBuffer);
        byteBuffer.flip();
        print("flipTest-flip-end",byteBuffer);
	}
	
	@Test
	public void rewindTest(){
		ByteBuffer byteBuffer = ByteBuffer.allocate(20).order(ByteOrder.BIG_ENDIAN);
		print("rewindTest-init-begin",byteBuffer);
		byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'H');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'i');  
        byteBuffer.put ((byte)0);  
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("rewindTest-put-7-end",byteBuffer);
        byteBuffer.limit(10);
        print("rewindTest-limit-10-end",byteBuffer);
        byteBuffer.put ((byte)'!');  
        byteBuffer.put ((byte)0);  
        print("rewindTest-put-2-end",byteBuffer);
        byteBuffer.rewind();
        print("rewindTest-rewind-end",byteBuffer);
	}
}
